<?php
class ThitruongkenhsController extends AppController {

	var $name = 'Thitruongkenhs';
    var $components = array('RequestHandler');

	function index($kenh_id=null) {
		$this->Thitruongkenh->recursive = 0;
		$this->set('thitruongkenhs',  $this->paginate(array('kenh_id'=>$kenh_id)));
        $kenhs=$this->Thitruongkenh->query('select * from kenhs where id='.$kenh_id);
		$this->set(compact('kenh_id','kenhs'));
	}

	function view($id = null) {
		if (!$id) {
			$this->Session->setFlash(__('Invalid thitruongkenh', true));
			$this->redirect(array('action' => 'index'));
		}
		$this->set('thitruongkenh', $this->Thitruongkenh->read(null, $id));
	}

	function add($kenh_id) {
		if (!empty($this->data)) {
			$this->Thitruongkenh->create();
			if ($this->Thitruongkenh->save($this->data)) {
				$this->Session->setFlash(__('Thông tin đã lưu', true));
				$this->redirect(array('action' => 'index',$kenh_id));
			} else {
				$this->Session->setFlash(__('Thông tin không thể lưu', true));
			}
		}
		$kenhs = $this->Thitruongkenh->Kenh->find('list');
		$this->set(compact('kenhs'));
	}

	function edit($id = null) {
		if (!$id && empty($this->data)) {
			$this->Session->setFlash(__('Invalid thitruongkenh', true));
			$this->redirect(array('action' => 'index'));
		}
		if (!empty($this->data)) {
			if ($this->Thitruongkenh->save($this->data)) {
				$this->Session->setFlash(__('Thông tin đã cập nhật', true));
				$this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('Thông tin không thể cập nhật', true));
			}
		}
		if (empty($this->data)) {
			$this->data = $this->Thitruongkenh->read(null, $id);
		}
		$kenhs = $this->Thitruongkenh->Kenh->find('list');
		$this->set(compact('kenhs'));
	}

	function delete($id = null,$kenh_id=null) {
		if (!$id) {
			$this->Session->setFlash(__('Invalid id for thitruongkenh', true));
			$this->redirect(array('action'=>'index',$kenh_id));
		}
		if ($this->Thitruongkenh->delete($id)) {
			$this->Session->setFlash(__('Thông tin đã xóa', true));
			$this->redirect(array('action'=>'index',$kenh_id));
		}
		$this->Session->setFlash(__('Thông tin không thể xóa', true));
		$this->redirect(array('action' => 'index',$kenh_id));
	}
    
    function update_thitruong(){
        if ($this->data) {
            App::import('Core', 'sanitize');
            $title = Sanitize::clean($this->data['Thitruongkenh']['thitruong']);
            $this->Thitruongkenh->id = $this->data['Thitruongkenh']['id'];
            $this->Thitruongkenh->saveField('thitruong', $title);
            $this->set('posttitle', $title);
        }
    }
    
    function update_ghichu(){
        if ($this->data) {
            App::import('Core', 'sanitize');
            $title = Sanitize::clean($this->data['Thitruongkenh']['ghichu']);
            $this->Thitruongkenh->id = $this->data['Thitruongkenh']['id'];
            $this->Thitruongkenh->saveField('ghichu', $title);
            $this->set('posttitle', $title);
        }
    }
}
